package org.dromara.manu.controller;

import cn.dev33.satoken.annotation.SaIgnore;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.dromara.common.sendgrid.service.SendGridEmailService;
import org.dromara.common.web.core.BaseController;
import org.dromara.manu.domain.vo.ManuPubToolScreenVo;
import org.dromara.manu.service.IManuPubToolScreenService;
import org.dromara.system.service.ISysOssService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.WebApplicationContext;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.thymeleaf.context.WebContext;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.springframework.ui.Model;

@RequiredArgsConstructor
@RestController
@RequestMapping("/manu/WhiteTool")
public class WhiteController extends BaseController {

    @Autowired
    private IManuPubToolScreenService manuPubToolScreenService;

    @Autowired
    private ISysOssService ossService;

    @Autowired
    private SendGridEmailService sgeSer;


    @SaIgnore
    @GetMapping("/getContentByScreenId/{id}")
    public String getScreenByScreenId(@PathVariable Long id, Model model){
        ManuPubToolScreenVo screen = manuPubToolScreenService.queryById(id);
        String category = screen.getCategory();
        Long fileId = screen.getFileId();
        String url = ossService.getById(fileId).getUrl();
        System.out.println("url = " + url);
        String viewName = "";
        switch (category) {
            case "Website":
                viewName = "ScreenViewer/WebsiteViewer";
                break;
            case "picture":
                viewName = "ScreenViewer/PictureViewer";
                break;
            case "ms_office":
                viewName = "ScreenViewer/MSOfficeViewer";
                break;
            case "pdf":
                viewName = "ScreenViewer/PDFViewer";
                break;
        }
        model.addAttribute("url", url);
        return viewName;
    }

    @SaIgnore
    @GetMapping("/getPicUrlByScreenId/{id}")
    @ResponseBody
    public String getPicUrlByScreenId(@PathVariable Long id){
        ManuPubToolScreenVo screen = manuPubToolScreenService.queryById(id);
        Long fileId = screen.getFileId();
        String url = ossService.getById(fileId).getUrl();
        return url;
    }

    @SaIgnore
    @GetMapping("/trial")
    public String tryTemplateMail(){
        String subject = "测试邮件";
        Map<String, Object> model = new HashMap<>();
        ArrayList<Object> materials = new ArrayList<>();
        materials.add(new HashMap<String, Object>(){{
            put("materialNumber", "014A0003");
            put("description", "测试物料");
            put("quantity", 100);
            put("deliveryDate", "");
        }});
        model.put("subject", subject);
        model.put("content", "这是一封测试邮件内容。");
        model.put("items", materials);
        model.put("supplierName", "白屏测试");
        sgeSer.sendTemplateMailTrial("bin.liu@danfoss.com", subject, model);
        return "trial";
    }

}
